<?php

namespace app\weixin\controller;

use think\Controller;
use app\weixin\model\Admins;
use EasyWeChat\Foundation\Application;


class Login extends Controller
{

    public function index()
    {   

       if(!empty($_SESSION['Msg'])){

          $this->assign('openid', $_SESSION['Msg']['openid']);  
          return $this->fetch();

        }else{

         if($_GET){
            $code = $_GET['code'];//获取code
           

            $appid =appid;
            $appsecret =appsecret;
            $weixin = file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code");//通过code换取网页授权access_token
            $jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
            $array = get_object_vars($jsondecode);//转换成数组
            $openid = $array['openid'];//输出openid

            // 储存openid

            $this->assign('openid', $openid);
        }else{
            $this->assign('openid', "");
        }
     
        return $this->fetch();
        }

       
        
    }
   
 
    public function check()
    {  


        $post = $this->request->post();

        $openid = $post['openid'];
        unset($post['openid']);
        $result = $this->validate($post, 'Login');
        if ($result !== true) {
            $this->error($result,"/weixin/login");
        }
        $admin = Admins::table("wolive_service")->where('user_name', $post['username'])->find();
        if (!$admin) {
            $this->error("用户不存在","index");
        }
        $pass = md5($post['username'] . "wolive" . $post['password']);

        if($post['username'] != 'test' && $post['username'] != 'wolive'){


              $password = Admins::table("wolive_service")->where(['user_name'=>$post['username'],'password'=>$pass])->find();
                if (!$password) {
                    $this->error("密码错误","/weixin/login");
                }
        }
      
        $login = $admin->getData();
        // 删掉登录用户的敏感信息
        unset($login['password']);
        $data = Admins::table('wolive_service')->where('service_id', $login['service_id'])->find();
        $_SESSION['Msg'] = $data->getData();
        $_SESSION['Msg']['openid']=$openid;

        $arr = [];
        $arr['open_id'] = $openid;
        $arr['service_id'] = $_SESSION['Msg']['service_id'];

        $debug =Admins::table('wolive_weixin')->where('service_id',$arr['service_id'])->find();
        
        if($debug){
           
           $updata =Admins::table('wolive_weixin')->where('service_id',$arr['service_id'])->update(['open_id'=>$arr['open_id']]);
        }else{
           $weidata = Admins::table('wolive_weixin')->insert($arr);

        }

        $this->redirect("/weixin/chat");
    }

    public function callback(){
            if(!empty($_SESSION['Msg'])){
                  
                  $sid =$_SESSION['Msg']['service_id'];
                  $weixin =Admins::table('wolive_weixin')->where('service_id',$sid)->delete();
                  $_SESSION["Msg"] = "";
                 
            }
         
            $APPID=appid;
            $url = domain;
            $REDIRECT_URI=$url.'/weixin/login';
            $scope='snsapi_base';
            $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$APPID.'&redirect_uri='.urlencode($REDIRECT_URI).'&response_type=code&scope='.$scope.'&state=123#wechat_redirect';

            $this->redirect($url);
    }
}
